package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    SetMealMapper setMealMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> categories = setMealMapper.pageQuery(setmealPageQueryDTO);
        long total = categories.getTotal();
        List<SetmealVO> records = categories.getResult();
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 创建Setmeal对象，存储至setmeal表
        // 创建List<SetmealDish>对象，先获取存储的套餐id
        // 轮询插入套餐id，再在setmeal_dish表中插入多条菜品信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();
        dishList.forEach(dish -> dish.setSetmealId(setmealId));
        setMealDishMapper.addBatch(dishList);
    }

    @Override
    public void startOrStop(Integer status, long id) {
        Setmeal setmeal = Setmeal.builder().
                status(status).
                id(id).
                build();
        setMealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void edit(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);
        setMealMapper.update(setmeal);

        List<SetmealDish> nList = setmealVO.getSetmealDishes();
        Long setmealId = setmeal.getId();
        List<SetmealDish> oList = setMealDishMapper.getBySetmealId(setmealId);

        if (!nList.equals(oList)){
            setMealDishMapper.delete(setmealId);
            setMealDishMapper.addBatch(nList);
        }
    }

    @Override
    @Transactional
    public void deleteById(long[] ids) {
        // 删除套餐同时要删除套餐内的套餐菜品
        for (long id : ids){
            setMealDishMapper.delete(id);
            setMealMapper.deleteById(id);
        }
    }

    @Override
    public SetmealVO getById(long id) {
        Setmeal setmeal  = setMealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        List<SetmealDish> setmealDishes = setMealDishMapper.getBySetmealId(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public List<SetmealDish> getSetmealDish(long id) {
        return setMealDishMapper.getBySetmealId(id);
    }

}
